"""
Case Type   : 资源池化
Case Name   : 缩容备一，停止备二节点
Create At   : 2024.10
Owner       : chen
Description :
    1.执行缩容备一
    gs_dropnode -U USER -G GROUP -h hostlist
    2.工具交互前停止备2
    cm_ctl stop -n 3
Expect      :
    1.终止
    2.成功
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.sta1_node = Node('Standby1DbUser')
        self.db_host = self.sta1_node.db_host
        self.s_user = self.pri_node.ssh_user

    def test_shared_storage(self):
        text = f'''-----step1: 执行缩容备一 expect: 终止-----'''
        self.log.info(text)
        course = ComThread(self.pri_comsh.exec_dropnode, 
            args=(self.sta1_node, macro.DB_ENV_PATH, True,))
        course.setDaemon(True)
        course.start()

        text = f'''-----step2: 工具交互前停止备2 expect: 成功-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl('stop', '-n 3')
        self.log.info(res)
        self.assertIn(self.constant.cm_stop_node_success_msg, res)
        course.join()
        res = course.get_result()
        self.assertIn('Failed to execute the command', res)

    def tearDown(self):
        text = f'''-----恢复环境-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl('start', '-n 3')
        self.log.info(res)
        self.assertIn(self.constant.cm_start_node_success_msg, res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')